package com.csdj.controller;

import com.alibaba.fastjson.JSON;
import com.csdj.common.WebResult;
import com.csdj.pojo.Config;
import com.csdj.pojo.User;
import com.csdj.service.ConfigService;
import com.csdj.service.UserService;
import com.csdj.util.EncodeUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private UserService service;
    @Resource
    private ConfigService configService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @GetMapping("/show")
    public WebResult test(){
        return  new WebResult(200,"test for springboot").putData("name","cloud");
    }
    @RequestMapping("/select")
    public String show(){
        String userName="阿伟";
        String pwd="123456";
        System.out.println("");
        boolean login = service.login(userName, pwd, "1234.1.1", "2020-1-1");
        System.out.println(login);
        if(login){
            String sha = EncodeUtil.encodeBySha(pwd, "AcT5MgEslVwRS9DD65VLt29GM", 3);
            System.out.println(sha);
            List<User> users = service.selectAll(userName, pwd);
            String key="userId";
            long userId=0;
            for (User user : users) {
                 userId = user.getId();
            }
            //redis保存密码
            redisTemplate.opsForValue().set("sha", sha);
            redisTemplate.opsForValue().set(key,userId);
            Object o = redisTemplate.opsForValue().get(key);
            Object s = redisTemplate.opsForValue().get("sha");
            redisTemplate.delete(key);
            redisTemplate.delete("sha");
            System.out.println(o);
            System.out.println(s);
            return "登录成功";
        }else{
            return "登录失败";
        }
    }
    @RequestMapping("/insert")
    public String insert(){
        User user=new User();
        user.setUsername("凯");
        user.setPassword("123456");
        user.setUserType(1);
        user.setReadName("徐凯");
        user.setNickName("牛马凯");
        user.setLastTm(Date.valueOf("2021-1-1"));
        user.setStatus(1);
        user.setRole(1);
        user.setRemoteIp("172.16.3.99");
        int i = service.insertUser(user);
        if(i>0){
            return "注册成功";
        }else {
            return "注册失败";
        }
    }
    @RequestMapping("selectAllByRole")
    public String selectAllByRole(){
        int role=1;
        List<User> users = service.selectAllByRole(role);
        String json= JSON.toJSONString(users);
        return json;
    }
    public String updatePwd(){
        Integer id=1;
        String pwd="123";
        Integer integer = service.updatePwd(pwd, id);
        if(integer>0){
            return "修改失败";
        }else{
            return "修改失败";

        }
    }
    @RequestMapping("updateConfig")
    public String updateConfig(){
        Config config=new Config();
        config.setType("mybatis");
        config.setKey("datasouce");
        config.setValue("127.0.0.1");
        config.setStatus(1);
        Integer integer = configService.updateConfig(config);
    if (integer>0){
        return "修改配置文件成功";
    }else{
        return "修改配置文件失败";
    }
    }
    @RequestMapping("insertConfig")
    public String insertConfig(){
        Config config=new Config();
        config.setType("spring");
        config.setKey("url");
        config.setValue("啦啦啦啦啦");
        config.setStatus(1);
        Integer integer = configService.insertConfig(config);
        if(integer>0){
            return "新增成功";
        }else{
            return "新增失败";
        }
    }
    @RequestMapping("selectAllByType")
    public String selectAllByType(){
        List<Config> mybatis = configService.selectAllByType("mybatis");
        System.out.println(mybatis.toString());
        return "根据类型查询所有配置信息";
    }
    @RequestMapping("selectPage")
    public String selectPage(){
        PageInfo<Config> configPageInfo = configService.selectPage(0);
        System.out.println(configPageInfo.toString());
        return "获取所有配置（分页）";
    }
    @RequestMapping("selectAllByTypeAndKey")
    public String selectAllByTypeAndKey(){
        List<Config> configs = configService.selectAllByTypeAndKey("mybatis", "pwd");
        System.out.println(configs.toString());
        return "根据key,type,获取配置";
    }
}
